const { host } = require('../utils/host')
const getSign = require('../utils/getSign')
const { getConfig } = require('../config')
const Ocr = async (filePath, data = { classify: 'BankCard' },  config = { headers: {}, baseurl: ''}) => {
  if (!filePath) {
    return Promise.reject(new Error(`filePath Can not be empty`))
  }
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `${config.baseurl || host()}/zeus/util/v1/ocr/simple`,
      filePath: filePath,
      name: 'file',
      header: { ...getConfig().requestHeader, ...config.headers, ...getSign()},
      formData: data,
      success: (res) => {
        let req = JSON.parse(res.data);
        if (req.code === 0) {
          resolve(req.data)
        } else {
          rError(req)
          reject(req)
        }
      },
      fail: (err) => {
        uni.showToast({ title: err.errMsg, icon: 'none',duration:2000 });
        reject(err)
      },

    })
    const rError = (res) => {
      uni.showToast({ title: res.msg, icon: 'none' });
    }

  })

}




module.exports = { Ocr };